package com.wlz.file;

import java.io.*;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * @author wlz
 * @date 2022-11-10  3:45 下午
 */
public class UpsFileZipTest {

    public static void main(String[] args) throws IOException {
        String filepath = "/Users/wlz/Desktop/HUMA00119270S000030C62XS000040/HUMA00119270S000030C62XS000040.zip";
        String dir = "/Users/wlz/Desktop/unzip/";
//        unZipFiles(new File(filepath), dir);
        UnZipFiles(new File(filepath), dir, null, null);
    }

    /**
     * 解压文件
     *
     * @param zipFile：需要解压缩的文件
     * @param descDir：解压后的目标目录
     */
    public static void unZipFiles(File zipFile, String descDir) throws IOException {
        File destFile = new File(descDir);
        if (!destFile.exists()) {
            destFile.mkdirs();
        }
        // 解决zip文件中有中文目录或者中文文件
        ZipFile zip = new ZipFile(zipFile, Charset.forName("GBK"));
        for (Enumeration entries = zip.entries(); entries.hasMoreElements(); ) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            InputStream in = zip.getInputStream(entry);
            String curEntryName = entry.getName();
            // 判断文件名路径是否存在文件夹
            int endIndex = curEntryName.lastIndexOf('/');
            // 替换
            String outPath = (descDir + curEntryName).replaceAll("\\*", "/");
            if (endIndex != -1) {
                File file = new File(outPath.substring(0, outPath.lastIndexOf("/")));
                if (!file.exists()) {
                    file.mkdirs();
                }
            }

            // 判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
            File outFile = new File(outPath);
            if (outFile.isDirectory()) {
                continue;
            }
            OutputStream out = new FileOutputStream(outPath);
            byte[] buf1 = new byte[1024];
            int len;
            while ((len = in.read(buf1)) > 0) {
                out.write(buf1, 0, len);
            }
            in.close();
            out.close();
        }
        System.out.println("解压 " + zipFile.getName() + " 成功");
    }


    /**
     * 解压文件到指定目录
     *
     * @param zipFile 文件路径
     * @param descDir 存储路径
     * @throws IOException
     */
    public static String UnZipFiles(File zipFile, String descDir, File sFoder, Integer type) {

        InputStream in = null;
        OutputStream out = null;
        ZipFile zip = null;

        try {
            File souFile = new File(descDir);
            if (!souFile.exists()) {
                souFile.mkdirs();
            }
            //解压zip文件中有中文目录或者中文文件
            zip = new ZipFile(zipFile, Charset.forName("GBK"));
            StringBuffer stringBuffer = new StringBuffer();
            for (Enumeration entries = zip.entries(); entries.hasMoreElements(); ) {
                ZipEntry entry = (ZipEntry) entries.nextElement();
                String zipEntryName = entry.getName();
                in = zip.getInputStream(entry);
                String outPath = descDir + File.separator + zipEntryName;
                //判断路径是否存在,不存在则创建文件路径
                File file = new File(outPath.substring(0, outPath.lastIndexOf(File.separator)));
                if (!file.exists()) {
                    file.mkdirs();
                }
                //判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
                if (new File(outPath).isDirectory()) {
                    continue;
                }
                //输出文件路径信息
                System.out.println("解压文件路径:" + outPath);
                out = new FileOutputStream(outPath);
                byte[] buf1 = new byte[1024];
                int len;
                while ((len = in.read(buf1)) > 0) {
                    out.write(buf1, 0, len);
                }
                File tempFile = new File(outPath);
                String result = null;
                //文件名称
//                if (fileTaskPackageEntity.getTaskPackageType() == 2) {
//                    result = compareFileV2(tempFile, sFoder, fileTaskPackageEntity, type);
//                } else {
//                    if (fileTaskPackageEntity.getPartTypeCategory() != null
//                            && fileTaskPackageEntity.getPartTypeCategory() != 1
//                            && fileTaskPackageEntity.getPartTypeCategory() != 0) {
//                        result = compareFileV2(tempFile, sFoder, fileTaskPackageEntity, type);
//                    } else {
//                        result = compareFile(tempFile, sFoder);
//                    }
//                }
                stringBuffer.append(result);
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
            }
            if (zip != null) {
                zip.close();
            }
            System.out.println("解压文件完毕");
//            FileUtils.deleteDirectory(new File(descDir));
            return stringBuffer.toString();
        } catch (Exception e) {
            System.out.println(String.format("解压控制器包失败。错误信息：%s,zip名称：%s", e, zipFile.getAbsolutePath()));
            e.printStackTrace();
            try {
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
                if (zip != null) {
                    zip.close();
                }
            } catch (IOException e1) {
                return "123";
            }
            return "123";
        }
    }
}
